package org.deri.socializer.activities.map;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.deri.socializer.NotificationPanel;
import org.deri.socializer.com.http.Walker;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

public class WalkerLocationsOverlay extends ItemizedOverlay<OverlayItem> {
 
	private List<WalkerOverlayItem> walkerItems;
	private Drawable marker;
	private GoogleMapsActivity googleMapsActivity;
	private NotificationPanel popupPanel;
	
	
	public WalkerLocationsOverlay(Drawable defaultMarker, GoogleMapsActivity googleMapsActivity) {
		super(defaultMarker);
		walkerItems = new ArrayList<WalkerOverlayItem>();
		marker = defaultMarker;
		this.googleMapsActivity = googleMapsActivity;
		this.popupPanel = googleMapsActivity.getPopupPanel();
		populate();
	}
 
	protected OverlayItem createItem(int index) {
		return (OverlayItem)walkerItems.get(index);
	}
 
	public int size() {
		return walkerItems.size();
	}
 
	public void clear() {
		this.walkerItems.clear();
		setLastFocusedIndex(-1);
		populate();
	}
	
	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.google.android.maps.ItemizedOverlay#draw(android.graphics.Canvas,
	 * com.google.android.maps.MapView, boolean)
	 */
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		
		Projection projection = mapView.getProjection();
        
		Iterator<WalkerOverlayItem> walkerIter = walkerItems.iterator();
		WalkerOverlayItem walkerItem;
		while (walkerIter.hasNext()) {
			walkerItem = walkerIter.next();
			
			int obfuscateRadius = walkerItem.getWalker().getObfuscateRadius();
			if (obfuscateRadius > 0) {
				this.addObfuscateRadius(canvas, projection, walkerItem);
			}
		}
			
		super.draw(canvas, mapView, shadow);
		boundCenterBottom(marker);
	}
 
	public void addItem(WalkerOverlayItem walkerItem) {
		walkerItems.add(walkerItem);
		setLastFocusedIndex(-1);
		populate();
	}
 
	
	private void addObfuscateRadius(Canvas canvas, Projection projection, WalkerOverlayItem walkerItem) {
		Point pt = new Point();
		projection.toPixels(walkerItem.getPoint(),pt);
		
		// derive radius - depends on resolutions
        GeoPoint newGeos = new GeoPoint(walkerItem.getPoint().getLatitudeE6()+(walkerItem.getWalker().getObfuscateRadius()),
        								walkerItem.getPoint().getLongitudeE6()); // adjust your radius accordingly
        Point pt2 = new Point();
        projection.toPixels(newGeos,pt2);
        float circleRadius = Math.abs(pt2.y-pt.y);

        Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        circlePaint.setColor(0x30000000);
        circlePaint.setStyle(Style.FILL_AND_STROKE);
        canvas.drawCircle((float)pt.x, (float)pt.y, circleRadius, circlePaint);

        circlePaint.setColor(0x99000000);
        circlePaint.setStyle(Style.STROKE);
        canvas.drawCircle((float)pt.x, (float)pt.y, circleRadius, circlePaint);

        //Bitmap markerBitmap = BitmapFactory.decodeResource(this.googleMapsActivity.getApplicationContext().getResources(),R.drawable.map_pin_red);
        //canvas.drawBitmap(markerBitmap,pt.x,pt.y-markerBitmap.getHeight(),null);
	}
	
	
	@Override
	protected boolean onTap(int index) {
	    WalkerOverlayItem item = this.walkerItems.get(index);
	    MapView map = this.googleMapsActivity.getMap();
	    
	    this.popupPanel.hide();

	    GeoPoint geo = item.getPoint();
	    Point pt = map.getProjection().toPixels(geo, null);
	    
	    Walker walker = item.getWalker();
	    boolean alignTop = pt.y*2>map.getHeight();
	    this.popupPanel.showWalkerActionNotification(this.googleMapsActivity, this.googleMapsActivity.getXmppClient(), walker, alignTop);
	    return true;
	 }

}